package main

import (
	"gitee.com/ling-bin/network/netInterface"
	"gitee.com/ling-bin/network/netService"
	"log"
	"runtime"
	"time"

	"fmt"
)

func main() {
	addrs := []string{"127.0.0.1", "192.168.1.34"}
	port := []string{"7015", "7016", "7017", "7018", "7019", "7020"} //监听端口
	addrAry := make([]string, 0, 5)
	for _, address := range addrs {
		for _, val := range port {
			addrAry = append(addrAry, address+":"+val)
		}
	}
	config := netService.DefaultConfig("udp", addrAry)
	config.BufferSize = 1024
	config.KeepTime = time.Second * 20
	config.HDataCache = true
	service := netService.NewUdpService(config)
	service.SetOnConnStart(func(connection netInterface.IConnection) {
		connId := connection.GetConnId()
		fmt.Println("[", connection.GetRemoteAddr().String(), "]新连接：", connId)
	})
	service.SetOnConnStop(func(connection netInterface.IConnection) {
		//connId := connection.GetConnId()
		//fmt.Println("[", connection.GetRemoteAddr().String(), "]连接关闭：", connId)
	})
	service.SetHandleStrategy(func(connection netInterface.IConnection, bytes []byte) *netInterface.StrategyData {
		key := string(bytes[1:13])
		return &netInterface.StrategyData{
			Key:       key,
			Receivers: nil,
			ExtData:   nil,
		}
	})
	//设置接收到完整包回调
	service.SetOnReceive(func(connection netInterface.IConnection, data []byte) {
		connId := connection.GetConnId()
		fmt.Println("[", connection.GetRemoteAddr().String(), "][", connId, "]连接上传新数据：", len(data), "---", string(data))
		//time.Sleep(time.Second * 15)
		//connId := connection.GetConnId()
		//fmt.Println("[", connection.GetRemoteAddr().String(), "][", connId, "]连接上传新数据：", len(data),"=>", string(data))
		//data1 := make([]byte,10000)
		//data1[0]= '('
		//data1[9999] = ')'
		//sendBf := []byte(fmt.Sprint("(udp ", connId, " service  ",len(data),"!)"))
		//err := connection.SendData(sendBf, "")
		//if err != nil {
		//	fmt.Println("下发异常：", err)
		//}
		//fmt.Println("启动时间：",connection.GetStartTime())
		//fmt.Println("心跳时间：",connection.GetHeartTime())
		//fmt.Println("上行信息：")
		//fmt.Println(connection.GetRecInfo())
		//fmt.Println("下行信息：")
		//fmt.Println(connection.GetRepInfo())
	})
	//设置下发成功回调
	service.SetOnReply(func(connection netInterface.IConnection, data []byte, isOk bool, cmdCode string, param interface{}, err error) {
		//connId := connId := connection.GetConnId()
		//fmt.Println("[",connection.GetRemoteAddr().String(),"][" , connId , "]连接下发数据[",connection.GetPackCount(),"]：" , len(data),"---",string(data))
		if !isOk {
			log.Println("数据下发异常：", err)
		}
		//fmt.Println("全局回调：",param)
	})

	service.SetLogHandle(func(level netInterface.ErrLevel, msg ...interface{}) {
		switch level {
		case netInterface.Fatal:
			log.Println("[致命]", msg)
			break
		case netInterface.Error:
			log.Println("[错误]", msg)
			break
		case netInterface.Warn:
			log.Println("[警告]", msg)
			break
		case netInterface.Info:
			log.Println("[消息]", msg)
			break
		}
	})

	go func() {
		for {
			log.Println(
				"[---UDP---]启动时间:", service.GetStartTime().Format("2006-01-02 15:04:05"),
				" 连接数:", service.GetConnMgr().Count(),
				" 协程数：", runtime.NumGoroutine())
			var toMap = service.ToMap()
			for key, val := range toMap {
				log.Println(key, ":", val)
			}
			time.Sleep(time.Second * 2)
		}
	}()
	service.Start()
	select {}
}
